package mmc.util;


public class APSCounter {
	private long lastCalc;
	private double aps = 0;
	private long occurences = 0;
	private float maxRecordedDT = 0;
	private String descripion;
	
	public APSCounter(String descripion){
		this.descripion = descripion;
		lastCalc = System.nanoTime();
	}
	
	public void notify(float dt, long occurenceCount){
		long now = System.nanoTime();
		
		occurences+=occurenceCount;
		if(dt>maxRecordedDT){
			maxRecordedDT = dt;
		}
		
		if(now - lastCalc > 1e9){
			aps = occurences / ((now - lastCalc)/1e9);
			lastCalc = now;
			occurences = 0;
			maxRecordedDT = 0;
		}
	}
	
	public double getAps(){
		return aps;
	}
	
	public String toString(){
		if(maxRecordedDT!=0)
			return String.format("%.2f",aps)+" "+descripion+", maxDT="+maxRecordedDT;
		else
			return String.format("%.2f",aps)+" "+descripion;
	}
}
